package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.PkGroup;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

public class PkVideoRankFragment extends BaseListFragment<PkVideoRankFragment.PkVideoRankVideoHoder, VideoCard> {
    private PkGroup pkGroup;


    public PkVideoRankFragment() {
    }

    public interface PkVideoListener {
        public void onPkVideoClick(VideoCard video);
    }

    /**
     * 创建fragment
     *
     * @param pkGroup
     * @return
     */
    public static PkVideoRankFragment create(PkGroup pkGroup) {
        PkVideoRankFragment fragment = new PkVideoRankFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("pkgroup", pkGroup);
        fragment.setArguments(bundle);
        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pkGroup = (PkGroup) getArguments().getSerializable("pkgroup");
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<VideoCard>>(new TypeReference<Result<List<VideoCard>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();

            }

            @Override
            public void responseInfo(Result<List<VideoCard>> data) {
                addResult(data);
            }
        });
    }


    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected PkVideoRankVideoHoder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_rank_pk_video, parent, false);
        return new PkVideoRankVideoHoder(view, new PkVideoListener() {
            @Override
            public void onPkVideoClick(VideoCard video) {
                forwardDetail(video.cardid);
            }
        });
    }

    public void forwardDetail(Long cardid) {
        Intent intent = new Intent(getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", cardid);
        startActivity(intent);
    }


    @Override
    protected PkVideoRankVideoHoder createfootViewHolder(View view) {
        return new PkVideoRankVideoHoder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_pk_video_rank_list, pkGroup.pid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public static class PkVideoRankVideoHoder extends BaseRecycleViewHolder<VideoCard> {
        private PkVideoListener pkVideoListener;
        private TextView mRank;
        private NetworkImageView mVideo;
        private TextView mNickName;
        private TextView mZan;
        private View itemView;

        public PkVideoRankVideoHoder(View itemView, PkVideoListener listener) {
            this(itemView);
            this.pkVideoListener = listener;
        }

        public PkVideoRankVideoHoder(View itemView) {
            super(itemView);
            mRank = (TextView) itemView.findViewById(R.id.tv_rank);
            mVideo = (NetworkImageView) itemView.findViewById(R.id.iv_video);
            mNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mZan = (TextView) itemView.findViewById(R.id.tv_zan);
            this.itemView = itemView;
        }

        @Override
        public void renderView(final VideoCard data, int position) {
            int rank = position + 1;
            if (rank == 1) {
                mRank.setText("");
                mRank.setBackgroundResource(R.drawable.video_no1);
            } else if (rank == 2) {
                mRank.setText("");
                mRank.setBackgroundResource(R.drawable.video_no2);
            } else if (rank == 3) {
                mRank.setText("");
                mRank.setBackgroundResource(R.drawable.video_no3);
            } else {
                mRank.setText((position + 1) + "");
                mRank.setBackgroundResource(0);
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pkVideoListener.onPkVideoClick(data);
                }
            });

            this.mVideo.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());
            this.mNickName.setText(data.user.nickname);
            this.mZan.setText(data.videoAttach.praisenum + "");

        }
    }

}

